﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core.Repositories;
using BlogMvc.Ultities;

namespace BlogMvc.Controllers
{
    public class HomeController : Controller
    {
        PostRepository _storeDB;

        public HomeController()
        {
            _storeDB = new PostRepository();
        }
        #region ver_0.1 Code First
        //public ActionResult Index(int skipAt = 0)
        //{
        //    var posts = _storeDB.Get();

        //    ViewBag.CurrentSkipAt = skipAt;
        //    ViewBag.PageSize = Config.PageSize;
        //    ViewBag.TotalRecords = posts.Count();
            
        //    posts = posts.Skip(skipAt).Take(Config.PageSize);

        //    return View(posts.ToList());
        //}

        //public ActionResult Details(int id)
        //{
        //    var post = _storeDB.GetById(id);
        //    return View(post);
        //}
        #endregion

        #region ver_0.2 Database First
        public ActionResult Index(int skipAt = 0)
        {
            var posts = _storeDB.GetAll().OrderByDescending(t => t.CreatedDate).ThenByDescending(t => t.ModifyDate).AsQueryable();

            ViewBag.CurrentSkipAt = skipAt;
            ViewBag.PageSize = Config.PageSize;
            ViewBag.TotalRecords = posts.Count();

            posts = posts.Skip(skipAt).Take(Config.PageSize);

            return View(posts.ToList());
        }

        public ActionResult Details(int id)
        {
            var post = _storeDB.GetOne(t => t.Id == id);
            return View(post);
        }
        #endregion
    }
}
